
from  datetime import datetime

class group_data:
    def __init__(self):
        self.id = 0;
        self.time = "";
        self.bid_index  = 0;
        self.max = 0
        self.count = 0
	self.max_time = 0.0
	self.line = 0
group_dict ={}
pre = group_data()

i = 0
time_idx = -1
group_idx = -1
bid_idx = -1
file_handle = open('raw.txt' , mode='w')
for line in open('sse_20210830_txt_dump', 'r'):
  values =  line.split(',')
  if values[0] =="td SSE":
      time_idx = 2
      group_idx = 5
      bid_idx = 11
  else if values[0] == "od SSE":
      time_idx = 2
      group_idx = 5
      bid_idx = 9
  else:
      continue
  curr = group_data()
  curr.id = values[group_idx]
  curr.time = values[time_idx]
  curr.bid_index = int(values[bid_idx])
  if (datetime.strptime(curr.time, '%H:%M:%S.%f')<datetime.strptime("09:30:00.000", '%H:%M:%S.%f')  or  datetime.strptime(curr.time, '%H:%M:%S.%f')> datetime.strptime("14:56:59.000", '%H:%M:%S.%f') ):
      continue

  if (curr.id in group_dict):
      pre = group_dict[curr.id]
      if (int(curr.bid_index) - int(pre.bid_index) < 0 ):
          diff = -(int(curr.bid_index) - int(pre.bid_index))
          if (diff > pre.max):
              pre.max = diff
          stamp_pre =pre.time 
          stamp_curr =curr.time 
          diff_time = (datetime.strptime(stamp_pre, '%H:%M:%S.%f') - datetime.strptime(stamp_curr, '%H:%M:%S.%f')).total_seconds() 
          if (diff_time > pre.max_time):
		pre.max_time = diff_time
          pre.count = pre.count+1
         # print("group,",int(curr.id) ,",back,",int(diff),",diff_time(s),",float(diff_time),",max_time,",float(pre.max_time),",max,",int(pre.max),",back count,",int(pre.count),",curr_bid,", int(curr.bid_index)  )
         # out_tuple = ("group",int(curr.id) ,"back",int(diff),"diff_time(s)",float(diff_time),"max_time",float(pre.max_time),"max",int(pre.max),"back count",int(pre.count),"curr_bid", int(curr.bid_index)  )
          out_tuple = ("group",str(curr.id) ,"back",str(diff),"diff_time(s)",str(diff_time),"max_time",str(pre.max_time),"max",str(pre.max),"back count",str(pre.count),"curr_bid", str(curr.bid_index),"line",str(pre.line), "curr_time", str(pre.time)  )
         # out = "group,"+str(curr.id) +",back,"+str(diff)+",diff_time(s),"+str(diff_time)+",max_time,"+str(pre.max_time)+",max,"+str(pre.max)+",back count,"+str(pre.count)+",curr_bid,"+str(curr.bid_index)+",line,"+str(pre.line)+ ",curr_time,"+str(pre.time) +"\n" 
#          print("group,%s ,back%s,diff_time,%s,max_time,%s,max,%s,back count,%s,curr_bid,%s,line,%s,curr_time,%s" %(str(curr.id) ,str(diff),str(diff_time),str(pre.max_time),str(pre.max),str(pre.count), str(curr.bid_index),str(pre.line), str(curr.time))  )
	  #file_handle.write(out)
	  print(','.join(out_tuple))
          pre.bid_index = curr.bid_index
          pre.time = curr.time
      else:
          pre.bid_index = curr.bid_index
          pre.time = curr.time
      pre.line = pre.line+1;
         
  else:
       group_dict[curr.id] = curr

file_handle.close()
